package com.ui.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bean.WxArticleAuthorBean;
import com.ddd.wandemo.R;

import java.util.ArrayList;
import java.util.List;

public class WxAuthorAdapter extends RecyclerView.Adapter<WxAuthorAdapter.SearchHistoryViewHolder> {
    private List<WxArticleAuthorBean> authorList = new ArrayList<>();
    private Context mContext;
    private WxAuthorAdapter.OnItemClickListener mListener;
    public WxAuthorAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public SearchHistoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_wx_author, parent, false);
        return new SearchHistoryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final SearchHistoryViewHolder holder, final int position) {
        holder.tvAuthor.setText(authorList.get(position).getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.itemClick(authorList.get(position).getId());
                holder.itemView.setSelected(true);
            }
        });
    }

    @Override
    public int getItemCount() {
        return authorList == null ? 0 : authorList.size();
    }

    class SearchHistoryViewHolder extends RecyclerView.ViewHolder {
        private TextView tvAuthor;
        public SearchHistoryViewHolder(View itemView) {
            super(itemView);
            tvAuthor = itemView.findViewById(R.id.tv_author);
        }
    }

    public void setData(List<WxArticleAuthorBean> data){
        authorList.clear();
        if (data != null) {
            authorList.addAll(data);
        }
        notifyDataSetChanged();
    }

    public interface OnItemClickListener{
        void itemClick(int id);
    }

    public void setOnItemClickListener(WxAuthorAdapter.OnItemClickListener listener){
        mListener = listener;
    }
}
